/*
 * $Id: conf.c,v 1.14 2010/04/05 11:32:18 fabio Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <ctype.h>
#include "conf.h"

void str_set(char **dest, char *src) {
    if (src == NULL) {
        *dest = NULL;
        return;
    }
    *dest = malloc (strlen(src) + 1);
    if (*dest == NULL) {
        printf("Not enough memory\n");
        exit (1);
    }
    strcpy (*dest, src);
}

void str_reset(char **dest, char *src) {
    if (*dest != NULL) {
        free (*dest);
    }
    str_set (dest, src);
}

int load_options(gpointer data) {
    gchar *conffile=".pomodoro.conf";
    char buf[256],*h,*val;
    FILE *f;
    int lineno=0;
    ProgressData *pdata=(ProgressData *)data;

    bzero(pdata, sizeof(ProgressData));
    if (!(h = getenv("HOME")))
        return -1;
    snprintf(buf, sizeof(buf), "%s/%s", getenv("HOME"),conffile);
    if (!(f=fopen(buf, "r"))) {
        printf("config file '%s' not found!\n",buf);
        exit(0);
    }
    while(!feof(f)) {
        fgets(buf, sizeof(buf), f);
        if (!feof(f)) {
            lineno++;
            val = strchr(buf, '#');
            if (val) *val = '\0';
            while(strlen(buf) && (buf[strlen(buf) - 1] < 33))
                buf[strlen(buf) - 1] = '\0';
            if (!strlen(buf))
                continue;
            val = buf;
            while(*val) {
                if (*val < 33)
                    break;
                val++;
            }
            if (*val) {
                *val = '\0';
                val++;
                while(*val && (*val < 33)) val++;
            }
            if (!strcasecmp(buf, "pomodoro_secs"))
                pdata->p_secs=atoi(val);
            if (!strcasecmp(buf, "pomodoro_interrupt"))
                pdata->p_interrupt=atoi(val);
            if (!strcasecmp(buf, "start_hidden"))
                pdata->p_starthidden=atoi(val);
            if (!strcasecmp(buf, "show_countdown"))
                pdata->showcountdown=atoi(val);
            if (!strcasecmp(buf, "activity_mode"))
                pdata->activity_mode=atoi(val);
            if (!strcasecmp(buf, "twitter_integration"))
                pdata->twitter_integration=atoi(val);
            if (!strcasecmp(buf, "twitter_username"))
                pdata->twitter_username=strdup(val);
            if (!strcasecmp(buf, "twitter_password"))
                pdata->twitter_password=strdup(val);
            if (!strcasecmp(buf, "proxy"))
                pdata->proxy=strdup(val);
        }
    }
    fclose(f);
    return 0;
}

void log_message(char *message, ...) {
    FILE *logfile;
    time_t now=time(NULL);
    char *histfile=".pomodoro.history";
    char *ct=ctime(&now),*h,buf[256];
    va_list ap;

    if (!(h = getenv("HOME")))
        return ;
    snprintf(buf, sizeof(buf), "%s/%s",h,histfile);
    ct[strlen(ct)-1]='\0';
    logfile=fopen(buf,"a");
    if(!logfile) {
        perror("log");
        return;
    }
    fprintf(logfile,"%s [%d]: ",ct,getpid());
    va_start(ap, message);
    vfprintf(logfile,message,ap);
    va_end(ap);
    fprintf(logfile,"\n");
    fclose(logfile);
}
